package com.dst.note.outline;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dst.note.BaseActivity;
import com.dst.note.model.Note;
import com.jscheng.annotations.Route;
import com.dst.note.R;
import com.dst.note.utils.DateUtil;
import com.dst.note.utils.InputMethodManagerUtil;

import java.util.ArrayList;
import java.util.List;

@Route("outline")
public class HomeActivity extends BaseActivity
        implements OutLinePresenter.OutLineView, View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
    private final static String TAG = "OutLinesActivity";
    private OutLinePresenter mPresenter;
    private RecyclerView mRecyclerView;
    private LinearLayoutManager mLayoutManager;
    private OutLinesAdapter mRecyclerAdapter;
    private RelativeLayout mHeadDateLayout;
    private TextView mHeadDateText;
    private FloatNewButton mFloatButton;
    private SwipeRefreshLayout mSwipeLayout;
    private OutLineCenterDialog mCenterDialog;

    private ImageView mBackIv;
    private EditText mSearchEt;
    private ImageView mSearchIv;
    private ImageView mDelIv;
    private TextView mNoteTitleTv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        this.mPresenter = new OutLinePresenter();
        this.getLifecycle().addObserver(mPresenter);

        this.mHeadDateLayout = findViewById(R.id.outline_head_date);
        this.mHeadDateText = mHeadDateLayout.findViewById(R.id.date_text);
        this.mFloatButton = findViewById(R.id.float_new_button);
        this.mFloatButton.setOnClickListener(this);
        this.mSwipeLayout = findViewById(R.id.swipe_layout);
        this.mSwipeLayout.setOnRefreshListener(this);

        this.mRecyclerView = findViewById(R.id.outline_recyclerview);
        this.mLayoutManager = new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false);
        this.mRecyclerAdapter = new OutLinesAdapter(mPresenter, mRecyclerView, mLayoutManager);
        this.mRecyclerView.setLayoutManager(mLayoutManager);
        this.mRecyclerView.setAdapter(mRecyclerAdapter);
        this.mRecyclerView.addOnScrollListener(new ScrollChangeListener());

        mBackIv = findViewById(R.id.back_iv);
        mBackIv.setOnClickListener(this);
        mSearchEt = findViewById(R.id.search_et);
        mSearchIv = findViewById(R.id.search_iv);
        mSearchIv.setOnClickListener(this);
        mDelIv = findViewById(R.id.del_iv);
        mDelIv.setOnClickListener(this);
        mNoteTitleTv = findViewById(R.id.note_name);
        mSearchEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mDelIv.setVisibility(TextUtils.isEmpty(s) ? View.GONE : View.VISIBLE);
                if (TextUtils.isEmpty(s)) {
                    if (mRecyclerAdapter.getNotes() != list) {
                        mRecyclerAdapter.setDataChangeLoc(list);
                    }
                } else {
                    List<Note> searchList = new ArrayList<>();
                    for (Note bean : list) {
                        if (bean.getTitle().contains(s.toString()) || bean.getSummary().contains(s.toString())) {
                            searchList.add(bean);
                        }
                    }
                    mRecyclerAdapter.setDataChangeLoc(searchList);
                }

            }
        });
    }

    private List<Note> list;

    @Override
    public void setData(List<Note> notes) {
        list = notes;
        mSwipeLayout.setRefreshing(false);
        mRecyclerAdapter.setData(notes);
    }

    @Override
    public void showCenterDialog(String id) {
        if (mCenterDialog == null) {
            mCenterDialog = new OutLineCenterDialog(this, mPresenter);
        }
        mCenterDialog.show(id);
    }

    private void tapNewButton() {
        mPresenter.tapNew();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.float_new_button:
                tapNewButton();
                break;
            case R.id.back_iv:
                mNoteTitleTv.setVisibility(View.VISIBLE);
                mBackIv.setVisibility(View.GONE);
                mSearchEt.setVisibility(View.GONE);
                mSearchIv.setVisibility(View.VISIBLE);
                mDelIv.setVisibility(View.GONE);
                InputMethodManagerUtil.hideSoftInput(mSearchEt);
                addAnimation();
                break;
            case R.id.search_iv:
                mNoteTitleTv.setVisibility(View.GONE);
                mBackIv.setVisibility(View.VISIBLE);
                mSearchEt.setVisibility(View.VISIBLE);
                mSearchIv.setVisibility(View.GONE);
                mSearchEt.requestFocus();
                InputMethodManagerUtil.showIm(mSearchEt);
                removeAnimation();
                break;
            case R.id.del_iv:
                mDelIv.setVisibility(View.GONE);
                mSearchEt.setText("");
                break;
            default:
                break;
        }
    }

    @Override
    public void onRefresh() {
        mPresenter.reload();
    }

    private class ScrollChangeListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            updateHeadDateLayout();
        }
    }

    @Override
    public void onBackPressed() {
        if (mBackIv.getVisibility() == View.VISIBLE) {
            onClick(mBackIv);
            return;
        }
        super.onBackPressed();
    }

    private void updateHeadDateLayout() {
        long time = mRecyclerAdapter.getFirstVisibleDateTime();
        if (time > 0) {
            mHeadDateLayout.setVisibility(View.VISIBLE);
            mHeadDateText.setText(DateUtil.formatDate(time));
        } else {
            mHeadDateLayout.setVisibility(View.INVISIBLE);
        }
        mHeadDateLayout.setVisibility(View.GONE);
    }

    private void removeAnimation() {
        ObjectAnimator animator = new ObjectAnimator().ofFloat(mFloatButton, "alpha", 1, 0);
        animator.setDuration(500);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                mFloatButton.setEnabled(false);
            }
        });
        animator.start();
    }

    private void addAnimation() {
        ObjectAnimator animator = new ObjectAnimator().ofFloat(mFloatButton, "alpha", 0, 1);
        animator.setDuration(500);
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                mFloatButton.setEnabled(true);
            }
        });
        animator.start();
    }
}
